import 'dart:math';

import 'package:flutter/material.dart';

class RouletteWheelPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar: AppBar(
          title: Text('Roulette Wheel'),
        ),
        body: Center(
          child: RouletteWheel(
              items: ['动物王国','科技馆', '华谊小镇', '生态动物园', '贝拉小镇', '岳麓山',
                  '橘子洲头', '石燕湖']),
        ),
    );
  }
}


class RouletteWheel extends StatefulWidget {
  final List<String> items;

  RouletteWheel({required this.items});


  @override
  _RouletteWheelState createState() => _RouletteWheelState();
}

class _RouletteWheelState extends State<RouletteWheel> with SingleTickerProviderStateMixin {

  late AnimationController _controller;
  late Animation<double> _animation;
  double _angle = 0.0;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 6000),
      vsync: this,
    );

    _animation = CurvedAnimation(
      parent: _controller,
      curve: Curves.decelerate,
    );

    _animation.addListener(() {
      setState(() {
        _angle = _animation.value * 2 * pi;
      });
    });

    _animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        // 动画完成后的操作
        _controller.reset();
      }
    });
  }

  void _spin() {
    _controller.forward(from: 0.0);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Transform.rotate(
          angle: _angle,
          child: CustomPaint(
            size: Size(300, 300),
            painter: RouletteWheelPainter(widget.items),
          ),
        ),
        SizedBox(height: 20),
        ElevatedButton(
          onPressed: _spin,
          child: Text('Spin'),
        ),
      ],
    );
  }
}

class RouletteWheelPainter extends CustomPainter {
  final List<String> items;

  RouletteWheelPainter(this.items);

  @override
  void paint(Canvas canvas, Size size) {
    final double radius = size.width / 2;
    final double centerX = size.width / 2;
    final double centerY = size.height / 2;
    final double sweepAngle = 2 * pi / items.length;

    final Paint paint = Paint()
      ..style = PaintingStyle.fill
      ..strokeWidth = 2.0;

    for (int i = 0; i < items.length; i++) {
      final double startAngle = i * sweepAngle;
      final double endAngle = startAngle + sweepAngle;

      // 设置颜色
      paint.color = i % 2 == 0 ? Colors.blue : Colors.red;

      // 绘制扇形
      canvas.drawArc(
        Rect.fromCircle(center: Offset(centerX, centerY), radius: radius),
        startAngle,
        sweepAngle,
        true,
        paint,
      );

      // 绘制文本
      final double textAngle = startAngle + sweepAngle / 2;
      final double textX = centerX + radius * 0.6 * cos(textAngle);
      final double textY = centerY + radius * 0.6 * sin(textAngle);

      TextPainter textPainter = TextPainter(
        text: TextSpan(
          text: items[i],
          style: TextStyle(color: Colors.white, fontSize: 16),
        ),
        textDirection: TextDirection.ltr,
      );

      textPainter.layout();
      textPainter.paint(
        canvas,
        Offset(textX - textPainter.width / 2, textY - textPainter.height / 2),
      );
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}